package com.cos.shop.web.controller;

import com.cos.shop.common.Const;
import com.cos.shop.entity.Cart;
import com.cos.shop.entity.User;
import com.cos.shop.services.CartServices;
import com.cos.shop.services.impl.CartServicesImpl;
import com.cos.shop.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/2/24 10:44
 * @description:
 */
@WebServlet("/cart")
public class CartController extends BaseServlet {
    private CartServices cs = new CartServicesImpl();
    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/24 14:11
     * @description: 修改购物车数量的请求
     */
    public String updateCartNum(HttpServletRequest request, HttpServletResponse response){
        //1 验证登录
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        if (u == null) {
            //用户没有登录
            return Const.REDIRECT+"login.jsp";
        }
        //2获取用户提交
        int pid = StringUtils.toInt(request.getParameter("pid"));
        int num = StringUtils.toInt(request.getParameter("num"));
        double prices = StringUtils.toDouble(request.getParameter("price"));
        //3 访问业务层
        cs.updateCart(u.getUid(),pid,num,prices);
        // 前端没有接受服务器的响应,所以直接返回空字符串
        return "";
    }

    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/24 13:58
     * @description: 删除删除车数据
     */
    public String clearCart(HttpServletRequest request, HttpServletResponse response){
        //1 验证登录
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        if (u == null) {
            //用户没有登录
            return Const.REDIRECT+"login.jsp";
        }
        //2 获取用户提交的数据
        int pid = StringUtils.toInt(request.getParameter("pid"));

        //3 范文业务层进行数据的删除
        boolean ans = cs.clearCart(pid,u.getUid());
        //4 直接返回到购物车的页面 先去获取数据
        return Const.REDIRECT+"cart?type=getCart";
    }


    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/24 11:42
     * @description: 查看用户购物车数据
     */
    public String getCart(HttpServletRequest request, HttpServletResponse response){
        //1 验证登录
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        if (u == null) {
            //用户没有登录
            return Const.REDIRECT+"login.jsp";
        }
        //2 直接访问业务层获取数据
        List<Cart> clist= cs.getCart(u.getUid());
        //3 放入reqeu作用域
        request.setAttribute("carts",clist);
        //4转发
        return Const.FORWARD+"cart.jsp";
    }

    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/24 10:45
     * @description: 添加购物车
     */
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        //1 验证登录
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        if (u == null) {
            //用户没有登录
            return Const.REDIRECT+"login.jsp";
        }
        //2 接受用户的参数
        int pid = StringUtils.toInt(request.getParameter("pid"));
        //3 访问业务层
        boolean ans = cs.createCart(u.getUid(),pid);
        //4 给出结果的响应
        if (ans) {
            return Const.REDIRECT+"cartSuccess.jsp";
        }
        request.setAttribute("msg", "购物车加入失败,原因:你穷|");
        return Const.FORWARD+"message.jsp";
    }


}
